package com.csu.csumall.controller.frontend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.csumall.common.UniformResponse;
import com.csu.csumall.entity.Product;
import com.csu.csumall.entity.ProductCategory;
import com.csu.csumall.service.IProductCategoryService;
import com.csu.csumall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.csu.csumall.common.Constant.ORIGIN;

@RestController
@RequestMapping("/api/recommend")
@CrossOrigin(origins = ORIGIN)
public class RecommendController {

    private final IProductService productService;
    private final IProductCategoryService productCategoryService;

    @Autowired
    public RecommendController(IProductService productService, IProductCategoryService productCategoryService) {
        this.productService = productService;
        this.productCategoryService = productCategoryService;
    }

    /**
     * 通过productId返回类似商品
     * @return
     */
    @GetMapping
    public ResponseEntity<UniformResponse<List<Product>>> get(@RequestParam(value = "productId", required = true)
                                                                          Integer productId,
                                                              @RequestParam(value = "size", required = true, defaultValue = "20")
                                                                      Integer size) {

//        按ProductId查所有记录，获得相关类别
        List<ProductCategory> productCategoryList1 = productCategoryService.list(
                new QueryWrapper<ProductCategory>().eq("product_id", productId)
        );
        QueryWrapper<ProductCategory> productCategoryQueryWrapper = new QueryWrapper<>();
        productCategoryList1.forEach(productCategory -> {
            productCategoryQueryWrapper.or().eq("category_id", productCategory.getCategoryId());
        });

//        按CategoryId查所有记录，获得所有商品
        List<ProductCategory> productCategoryList2 = productCategoryService.list(productCategoryQueryWrapper);
        Set<Integer> productIdHashSet = productCategoryList2.stream().map(ProductCategory::getProductId).collect(Collectors.toSet());
//        List<Product> productList = productService.listByIds(productIdHashSet);

        Page<Product> productPage = new Page<>(1, size);
        IPage<Product> productIPage = productService.page(
                productPage,
                new QueryWrapper<Product>().in("id", productIdHashSet)
        );
        List<Product> productList = productIPage.getRecords();
        return new ResponseEntity<>(UniformResponse.createForSuccess(productList), HttpStatus.OK);
    }
}
